package com.sprint.questai.controller;

import cn.hutool.http.HttpRequest;
import com.sprint.questai.common.BaseResponse;
import com.sprint.questai.common.ResultUtils;
import com.sprint.questai.model.dto.request.ChunkInsertRequest;
import com.sprint.questai.model.dto.request.UpdateChunkRequest;
import com.sprint.questai.model.dto.response.ChunkResponse;
import com.sprint.questai.model.entity.ChunkInfo;
import com.sprint.questai.model.entity.Index;
import com.sprint.questai.module.kb.KnowledgeService;
import org.apache.tomcat.util.http.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @Author: mayixiang
 * @Date: 2024/6/14 10:07
 * @Description: 知识库接口
 */
@RestController
@RequestMapping("/knowledge")
public class KnowledgeController {

    @Autowired
    private KnowledgeService knowledgeService;

    @PostMapping("/create")
    public BaseResponse<String> createKnowledge(HttpServletRequest request) throws IOException {
        HttpSession session = request.getSession();
        String userId =(String) session.getAttribute("userId");
        System.out.println(userId);
        String knowledge = knowledgeService.createKnowledge(userId);
        return ResultUtils.success(knowledge);
    }


    @PostMapping("/insertChunk")
    public BaseResponse<String> insertEmptyChunk(@RequestBody String knowledgeId) throws IOException {
        String s= knowledgeService.addEmptyChunk(knowledgeId);
        return ResultUtils.success(s);
    }
    public BaseResponse<String> insertChunk(@RequestBody ChunkInsertRequest request) throws IOException {
        String s = knowledgeService.addChunk(request.getKnowledgeId(), request.getChunk());
        return ResultUtils.success(s);
    }
    @GetMapping("/getKnowledges")
    public BaseResponse<List<Index>> getKnowledge(HttpServletRequest request) throws IOException {
        HttpSession session = request.getSession();
        String userId =(String) session.getAttribute("userId");
        List<Index> knowledgesByUserId = knowledgeService.getKnowledgesByUserId(userId);
        return ResultUtils.success(knowledgesByUserId);
    }
    @GetMapping("/getChunks")
    public BaseResponse<List<ChunkResponse>> getChunks(@RequestParam String knowledgeId) throws IOException {
        List<ChunkResponse> response = knowledgeService.searchChunksByKnowledgeIdResponse(knowledgeId);
        return ResultUtils.success(response);
    }

    @PutMapping("/updateChunk")
    public BaseResponse<String> updateChunk(@RequestBody UpdateChunkRequest request) throws IOException {
        String knowledgeId = request.getKnowledgeId();
        String chunkId = request.getChunkId();
        String updatedChunk = request.getChunk();
        String s = knowledgeService.updateChunkByChunkId(knowledgeId, chunkId, updatedChunk);
        return ResultUtils.success(s);
    }
}
